package com.dodo.blog.ui.page.admin;

import com.dodo.blog.model.Role;
import com.dodo.blog.model.Tag;
import com.dodo.blog.request.BasicRequest;
import com.dodo.blog.server.TagService;
import com.dodo.blog.ui.ajax.Response;
import com.dodo.blog.ui.ajax.annotation.AjaxRequest;
import com.dodo.blog.ui.authorization.Authorized;
import com.dodo.blog.ui.component.input.Anchor;
import com.dodo.blog.ui.component.input.Button;
import com.dodo.blog.ui.component.repeater.IDataSource;
import com.dodo.blog.ui.component.repeater.OrderDirection;
import com.dodo.blog.ui.component.repeater.Table;
import com.dodo.blog.ui.component.repeater.TableHeader;
import com.dodo.blog.ui.page.AbstractPage;
import com.dodo.blog.ui.util.FormatUtil;

import javax.inject.Inject;
import java.util.List;

/**
 * @author <a href="mailto:pohorelec@comvai.com">Jozef Pohorelec</a>
 */
@Authorized( roles = {Role.ADMIN} )
public class TagList
        extends AbstractPage
{
    private static final long serialVersionUID = 1L;

    private static TagService tagService;

    private static final String DELETE_TAG_METHOD = "deleteTag";

    @AjaxRequest( name = DELETE_TAG_METHOD )
    @SuppressWarnings( value = "unused" )
    public Response deleteTag()
    {
        tagService.deleteTag( getLongParameter( TagEdit.TAG_ID ) );

        return Response.redirect( TagList.class );
    }

    @Override
    public void onRender()
    {
        setHeader( localize( "header.tagList" ) );

        Table<Tag> table = new Table<Tag>( "tag-list-table", new TagDataSource() )
        {
            private static final long serialVersionUID = 1L;

            @Override
            public TableHeader[] getTableHeaders()
            {
                return new TableHeader[]{
                        new TableHeader( "name", localize( "label.name" ) ),
                        new TableHeader( "normalizedName", localize( "label.normalizedName" ) ),
                        new TableHeader( "createdDate", localize( "label.createdDate" ) ),
                        new TableHeader( "updatedDate", localize( "label.updatedDate" ) ),
                        new TableHeader(),
                };
            }

            @Override
            public void populate( com.dodo.blog.model.Tag tag )
            {
                addColumn( tag.getName() );
                addColumn( tag.getNormalizedName() );
                addColumn( tag.getCreatedDate(), FormatUtil.DATE_FORMAT_MONTH_YEAR );
                addColumn( tag.getUpdatedDate(), FormatUtil.DATE_FORMAT_MONTH_YEAR );
                addColumn(
                        new Anchor( "edit", localize( "button.edit" ), TagEdit.class ).addParameter( TagEdit.TAG_ID, tag.getId() ),
                        new Button( "delete", tag.getId().toString(), localize( "button.delete" ) ).setAjaxMethod( DELETE_TAG_METHOD )
                );
            }
        };
        add( table );

        add( new Anchor( "new-tag", localize( "button.new" ), TagEdit.class ) );
    }

    @Inject
    public void setTagService( TagService tagService )
    {
        TagList.tagService = tagService;
    }

    private class TagDataSource
            implements IDataSource<Tag>
    {
        private static final long serialVersionUID = 1L;

        @Override
        public List<Tag> getRows( int firstRow, int maxRows, String orderBy, OrderDirection direction )
        {
            BasicRequest request = new BasicRequest( firstRow, maxRows );
            request.addSort( orderBy, direction );
            return tagService.getTagList( request );
        }
    }
}
